package com.chinameyer.workorders.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chinameyer.workorders.bean.MeyerStaffTime;
import com.chinameyer.workorders.bean.MeyerWorkOrders;
import com.chinameyer.workorders.dto.orders.AddStaffAndTimeDTO;
import com.chinameyer.workorders.dto.orders.EditStaffAndTimeDTO;
import com.chinameyer.workorders.dto.orders.UpdateWorkOrdersDTO;
import com.chinameyer.workorders.dto.orders.AddWorkOrdersDTO;
import com.chinameyer.workorders.mapper.CatalogueMapper;
import com.chinameyer.workorders.mapper.StaffTimeMapper;
import com.chinameyer.workorders.mapper.WorkOrderMapper;
import com.chinameyer.workorders.vo.WorkOrders4AdminVO;
import com.chinameyer.workorders.vo.WorkOrders4StaffVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author meyer@HongYe
 */
@Service
public class WorkOrderService {

    @Resource
    private WorkOrderMapper workOrderMapper;
    @Resource
    private StaffTimeMapper staffTimeMapper;
    @Resource
    private CatalogueMapper catalogueMapper;


    public boolean checkThirdLevel(int catalogueId) {
        int cataloguePID = this.catalogueMapper.selectById(catalogueId).getPid();
        if (cataloguePID == 0) {
            return false;
        }
        int pid = this.catalogueMapper.selectById(cataloguePID).getPid();
        if (pid == 0) {
            return false;
        }
        return true;
    }

    @Transactional
    public void addOrders(AddWorkOrdersDTO addWorkOrdersDTO) {
        MeyerWorkOrders workOrders = new MeyerWorkOrders();
        BeanUtils.copyProperties(addWorkOrdersDTO,workOrders);
        this.workOrderMapper.insert(workOrders);
    }

    @Transactional
    public void deleteWorkOrders(Integer orderId) {
        this.workOrderMapper.deleteById(orderId);
        QueryWrapper<MeyerStaffTime> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(MeyerStaffTime::getOrderId,orderId);
        this.staffTimeMapper.delete(wrapper);
    }

    @Transactional
    public void editOrders(UpdateWorkOrdersDTO updateWorkOrdersDTO) {
        MeyerWorkOrders workOrders = this.workOrderMapper.selectById(updateWorkOrdersDTO.getId());
        BeanUtils.copyProperties(updateWorkOrdersDTO,workOrders);
        this.workOrderMapper.updateById(workOrders);
    }

    public List<WorkOrders4AdminVO> getAllWorkOrdersWithoutPaging(String startTime,String endTime) {
        QueryWrapper<MeyerWorkOrders> wrapper = new QueryWrapper<>();
        wrapper.lambda().orderByDesc(MeyerWorkOrders::getId).between(MeyerWorkOrders::getCreateTime,startTime,endTime);
        List<MeyerWorkOrders> workOrdersList = this.workOrderMapper.selectList(wrapper);
        List<WorkOrders4AdminVO> workOrders4AdminVOList = new ArrayList<>();
        this.copyProperties(workOrders4AdminVOList,workOrdersList);
        return workOrders4AdminVOList;
    }

    public List<WorkOrders4AdminVO> getAllWorkOrders(String productNo, String productName,Integer catalogueId, Integer count, Integer page) {
        List<MeyerWorkOrders> workOrdersList = this.workOrderMapper.selectAllWorkOrdersWithPaging(productNo,productName,catalogueId,count*page,count);
        List<WorkOrders4AdminVO> workOrders4AdminVOList = new ArrayList<>();
        this.copyProperties(workOrders4AdminVOList,workOrdersList);
        return workOrders4AdminVOList;
    }

    public int getAllWorkOrdersTotal(String productNo, String productName,Integer catalogueId) {
        return this.workOrderMapper.selectAllWorkOrdersWithoutPaging(productNo,productName,catalogueId);
    }

    private void copyProperties(List<WorkOrders4AdminVO> workOrders4AdminVOList, List<MeyerWorkOrders> workOrdersList) {
        workOrdersList.forEach(workOrders -> {
            WorkOrders4AdminVO workOrders4AdminVO = new WorkOrders4AdminVO();
            BeanUtils.copyProperties(workOrders, workOrders4AdminVO);
            QueryWrapper<MeyerStaffTime> wrapper = new QueryWrapper<>();
            wrapper.lambda().eq(MeyerStaffTime::getOrderId,workOrders.getId());
            List<MeyerStaffTime> staffTimeList = this.staffTimeMapper.selectList(wrapper);
            if (staffTimeList.size() != 0) {
                for (int i = 0;i < staffTimeList.size();i++) {
                    this.setStaffAndTime(i,staffTimeList.get(i), workOrders4AdminVO);
                }
            }
            workOrders4AdminVOList.add(workOrders4AdminVO);
        });
    }

    private void setStaffAndTime(int index, MeyerStaffTime staffTime, WorkOrders4AdminVO workOrders4AdminVO) {
        if (index == 0) {
            workOrders4AdminVO.setTime1(staffTime.getTime()).setStaff1(staffTime.getStaff());
        }
        else if (index == 1) {
            workOrders4AdminVO.setTime2(staffTime.getTime()).setStaff2(staffTime.getStaff());
        }
        else if (index == 2) {
            workOrders4AdminVO.setTime3(staffTime.getTime()).setStaff3(staffTime.getStaff());
        }
        else if (index == 3) {
            workOrders4AdminVO.setTime4(staffTime.getTime()).setStaff4(staffTime.getStaff());
        }
        else if (index == 4) {
            workOrders4AdminVO.setTime5(staffTime.getTime()).setStaff5(staffTime.getStaff());
        }
        else if (index == 5) {
            workOrders4AdminVO.setTime6(staffTime.getTime()).setStaff6(staffTime.getStaff());
        }
        else if (index == 6) {
            workOrders4AdminVO.setTime7(staffTime.getTime()).setStaff7(staffTime.getStaff());
        }
        else if (index == 7) {
            workOrders4AdminVO.setTime8(staffTime.getTime()).setStaff8(staffTime.getStaff());
        }
        else if (index == 8) {
            workOrders4AdminVO.setTime9(staffTime.getTime()).setStaff9(staffTime.getStaff());
        }
        else if (index == 9) {
            workOrders4AdminVO.setTime10(staffTime.getTime()).setStaff10(staffTime.getStaff());
        }
    }


    public boolean checkStaffAndTimeSize(AddStaffAndTimeDTO addStaffAndTimeDTO) {
        QueryWrapper<MeyerStaffTime> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(MeyerStaffTime::getOrderId,addStaffAndTimeDTO.getOrderId());
        return this.staffTimeMapper.selectCount(wrapper) >= 10;
    }

    @Transactional
    public int addStaffAndTime(AddStaffAndTimeDTO addStaffAndTimeDTO) {
        MeyerStaffTime staffTime = new MeyerStaffTime();
        BeanUtils.copyProperties(addStaffAndTimeDTO,staffTime);
        this.staffTimeMapper.insert(staffTime);
        return staffTime.getId();
    }

    @Transactional
    public void deleteStaffAndTime(Integer staffAndTimeId) {
        QueryWrapper<MeyerStaffTime> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(MeyerStaffTime::getId,staffAndTimeId);
        this.staffTimeMapper.delete(wrapper);
    }

    @Transactional
    public void editStaffAndTime(EditStaffAndTimeDTO editStaffAndTimeDTO) {
        MeyerStaffTime staffTime = this.staffTimeMapper.selectById(editStaffAndTimeDTO.getId());
        staffTime.setStaff(editStaffAndTimeDTO.getStaff()).setTime(editStaffAndTimeDTO.getTime());
        this.staffTimeMapper.updateById(staffTime);
    }

    public List<WorkOrders4StaffVO> getAll4Staff(String productNo, String productName,Integer catalogueId, Integer count, int page) {
        List<MeyerWorkOrders> workOrdersList = this.workOrderMapper.selectAllActiveWorkOrdersWithPaging(productNo,productName,catalogueId,count*page,count);
        List<WorkOrders4StaffVO> workOrders4StaffVOList  = new ArrayList<>();
        workOrdersList.forEach(workOrders -> {
            WorkOrders4StaffVO workOrders4StaffVO = new WorkOrders4StaffVO();
            QueryWrapper<MeyerStaffTime> wrapper = new QueryWrapper<>();
            wrapper.lambda().eq(MeyerStaffTime::getOrderId,workOrders.getId());
            List<MeyerStaffTime> staffTimeList = this.staffTimeMapper.selectList(wrapper);
            BeanUtils.copyProperties(workOrders,workOrders4StaffVO);
            workOrders4StaffVO.setStaffTimeList(staffTimeList);
            workOrders4StaffVOList.add(workOrders4StaffVO);
        });
        return workOrders4StaffVOList;
    }

    public int getAllActiveWorkOrdersTotal(String productNo, String productName,Integer catalogueId) {
        return this.workOrderMapper.selectAllActiveWorkOrdersWithoutPaging(productNo,productName,catalogueId);
    }

}
